﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Application.Admin.Pic
{
    public class OpenPicService : IOpenPicService
    {
        Application.Admin.Pic.IPicInfoService picService;

        Application.Admin.Pic.IPicThumbService thumbService;

        public OpenPicService(Application.Admin.Pic.IPicInfoService pic,
            Application.Admin.Pic.IPicThumbService thumb)
        {
            if (pic == null)
            {
                throw new ArgumentNullException("picService is null");
            }
            if (thumb == null)
            {
                throw new ArgumentNullException("thumbService is null");
            }
            picService = pic;
            thumbService = thumb;
        }

        public IQueryable<Domain.Entity.PicInfo> GetAllPic()
        {
            var data = picService.GetList();
            return data;
        }

        public Dto.OpenPicDto GetOpenPicById(int id)
        {
            Dto.OpenPicDto pic = null;
            var temp = picService.Get(id);
            if (temp != null)
            {
                pic = new Application.Admin.Dto.OpenPicDto();
                pic.Id = temp.Id;
                pic.Name = temp.Name;
                pic.WebFullPath = temp.WebFullPath;

                //pic.AddTime = temp.AddTime;
                //pic.IpAddress = temp.IpAddress;
                //pic.SortNum = temp.SortNum;
                //pic.State = temp.State;
                //pic.PhyPrePath = temp.PhyPrePath;
                //pic.Remark = temp.Remark;
                //pic.Source = temp.Source;
                //pic.SourceName = temp.SourceName;
                //pic.ViewCount = temp.ViewCount;
                //pic.WebPrePath = temp.WebPrePath;
                //pic.Width = temp.Width;
                //pic.Height = temp.Height;
            }
            return pic;
        }

        public List<Dto.OpenPicDto> GetOpenPicByIdList(IList<int> idList)
        {
            List<Application.Admin.Dto.OpenPicDto> data = new List<Dto.OpenPicDto>();
            var all = picService.GetList().Where(e => idList.Contains(e.Id) && e.State == true);
            Dto.OpenPicDto temp = null;
            foreach (var item in all)
            {
                temp = new Dto.OpenPicDto();
                temp.Id = item.Id;
                temp.Name = item.Name;
                temp.WebFullPath = item.WebFullPath;

                //temp.AddTime = item.AddTime;
                //temp.Height = item.Height;
                //temp.IpAddress = item.IpAddress;
                //temp.PhyPrePath = item.PhyPrePath;
                //temp.Remark = item.Remark;
                //temp.SortNum = item.SortNum;
                //temp.Source = item.Source;
                //temp.SourceName = item.SourceName;
                //temp.State = item.State;
                //temp.ViewCount = item.ViewCount;
                //temp.WebPrePath = item.WebPrePath;
                //temp.Width = item.Width;
                data.Add(temp);
            }
            return data;
        }

        public Dto.OpenPicThumbDto GetOpenPicThumbByPicId(int id)
        {
            var data = thumbService.GetList().Where(e => e.PicId == id).FirstOrDefault();
            if (data == null)
            {
                return null;
            }
            var temp = new Dto.OpenPicThumbDto();

            temp.Id = data.Id;
            temp.WebFullPath = data.WebFullPath;
            temp.Name = data.Name;
            temp.PicId = data.PicId;
            temp.Height = data.Height;
            temp.Width = data.Width;

            //temp.AddTime = data.AddTime;
            //temp.IpAddress = data.IpAddress;
            //temp.PhyPrePath = data.PhyPrePath;
            //temp.Remark = data.Remark;
            //temp.SortNum = data.SortNum;
            //temp.SourceName = data.SourceName;
            //temp.State = data.State;
            //temp.ViewCount = data.ViewCount;
            //temp.WebPrePath = data.WebPrePath;

            return temp;
        }

        public Dto.OpenPicThumbDto GetOpenPicThumbByPicId(int id, int width, int height)
        {
            var data = thumbService.GetList()
                .Where(e => e.PicId == id && e.Width == width && e.Height == height).FirstOrDefault();
            if (data == null)
            {
                return null;
            }
            var temp = new Dto.OpenPicThumbDto();
            temp.Id = data.Id;
            temp.Name = data.Name;
            temp.PicId = data.PicId;
            temp.WebFullPath = data.WebFullPath;
            temp.Width = data.Width;
            temp.Height = data.Height;

            //temp.AddTime = data.AddTime;
            //temp.IpAddress = data.IpAddress;
            //temp.PhyPrePath = data.PhyPrePath;
            //temp.Remark = data.Remark;
            //temp.SortNum = data.SortNum;
            //temp.SourceName = data.SourceName;
            //temp.State = data.State;
            //temp.ViewCount = data.ViewCount;
            //temp.WebPrePath = data.WebPrePath;

            return temp;
        }

        public List<Dto.OpenPicThumbDto> GetOpenPicThumbListByPicId(int id)
        {
            List<Application.Admin.Dto.OpenPicThumbDto> data = new List<Dto.OpenPicThumbDto>();
            var all = thumbService.GetList().Where(e => e.PicId == id);
            Dto.OpenPicThumbDto temp = null;
            foreach (var item in all)
            {
                temp = new Dto.OpenPicThumbDto();
                temp.Id = item.Id;
                temp.Name = item.Name;
                temp.PicId = item.PicId;
                temp.WebFullPath = item.WebFullPath;
                temp.Width = item.Width;
                temp.Height = item.Height;

                //temp.AddTime = item.AddTime;
                //temp.IpAddress = item.IpAddress;
                //temp.PhyPrePath = item.PhyPrePath;
                //temp.Remark = item.Remark;
                //temp.SortNum = item.SortNum;
                //temp.SourceName = item.SourceName;
                //temp.State = item.State;
                //temp.ViewCount = item.ViewCount;
                //temp.WebPrePath = item.WebPrePath;

                data.Add(temp);
            }
            return data;
        }
    }
}
